﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using Abp.Linq.Extensions;
using System.Threading.Tasks;
using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Stats.Dtos;
using Yozeev.Common;
using Microsoft.AspNetCore.Mvc;
using Abp.Domain.Entities.Auditing;
using Yozeev.UserManagerment.Users;
using Yozeev.Controllers.Stats.Filters;
using Yozeev.Dtos;

namespace Yozeev.Controllers.CheckTicket
{
    [Route("api/CheckTicket/[Controller]/[Action]")]
    public class RecordController : YozeevControllerBase
    {
        private readonly IRepository<TicketDetailHistory, Guid> _ticketDetailRepository;

        private readonly ITicketDetailManager _ticketDetailManager;

        private readonly IQueryHelper _queryHelper;

        private readonly IRepository<Device, Guid> _deviceRepository;

        private readonly IRepository<Boat, Guid> _boatRepository;

        private readonly AbstractFilter _deviceFilter;

        private readonly AbstractFilter _boatFilter;

        private readonly AbstractFilter _checkerFilter;

        private readonly AbstractFilter _ticketFilter;

        public RecordController(
            IRepository<TicketDetailHistory, Guid> ticketDetailRepository,
            IRepository<Device, Guid> deviceRepository,
            IRepository<Boat, Guid> boatRepository,
            ITicketDetailManager ticketDetailManager,
            DeviceFilter deviceFilter,
            BoatFilter boatFilter,
            CheckerFilter checkerFilter,
            TicketTypeFilter ticketFilter,
            IQueryHelper queryHelper)
        {
            _ticketDetailRepository = ticketDetailRepository;
            _deviceRepository = deviceRepository;
            _boatRepository = boatRepository;
            _ticketDetailManager = ticketDetailManager;
            _deviceFilter = deviceFilter;
            _boatFilter = boatFilter;
            _checkerFilter = checkerFilter;
            _ticketFilter = ticketFilter;
            _queryHelper = queryHelper;
        }

        /// <summary>
        /// 过闸记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<StatsPagedResultDto<GateRecordResultDto>> GetPagedStat(GateRecordInput input)
        {
            var ticketDetailHistories = _ticketDetailRepository.GetAll()
                .Where(_queryHelper.WhereExp<TicketDetailHistory>(input.queryData));
            
            var count = await ticketDetailHistories.CountAsync();
            var histories = await ticketDetailHistories
                .OrderBy(input.Sorting).AsNoTracking()
                .PageBy(input)
                .ToListAsync();
            List<GateRecordResultDto> result = new List<GateRecordResultDto>();
            foreach (var history in histories)
            {
                result.Add(new GateRecordResultDto
                {
                    User = history.Checker,
                    DeviceId = history.DeviceId == null ? "" : history.DeviceId.ToString(),
                    TicketName = history.TicketName,
                    TicketNo = history.TicketNo,
                    VisitValue = history.VisitValue,
                    StatusCode = history.StatusCode,
                    StatusText = history.StatusText,
                    CheckDate = history.CheckDate,
                    ScheduleCode = history.TicketDetail == null ? "" : history.TicketDetail.Schedule.ScheduleCode,
                    BoatName = history.TicketDetail == null ? "" : history.TicketDetail.Boat.BoatName
                });
            }
            Dictionary<string, IEnumerable<object>> filters = new Dictionary<string, IEnumerable<object>>();
            _deviceFilter.Decorate(filters);
            _boatFilter.Decorate(filters);
            _checkerFilter.Decorate(filters);
            _ticketFilter.Decorate(filters);
            return new StatsPagedResultDto<GateRecordResultDto>(count, result, filters); ;
        }

        /// <summary>
        /// 获取过闸记录的查询条件下拉框值
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<GateRecordFilters> Filters()
        {
            List<DeviceTypeEnum> deviceTypes = Enum.GetValues(typeof(DeviceTypeEnum))
                .OfType<DeviceTypeEnum>().ToList();
            List<Device> devices = await _deviceRepository.GetAll()
                .Where(d => d.IsEnabled).ToListAsync();
            List<TicketTypeEnum> ticketTypes = Enum.GetValues(typeof(TicketTypeEnum))
                .OfType<TicketTypeEnum>().ToList();
            List<Boat> boats = await _boatRepository.GetAll()
                .Where(b => b.RunStatus == RunStatusEnum.Running).ToListAsync();
            List<User> operators = new List<User>();
            List<CheckStatusEnum> checkStatuses = Enum.GetValues(typeof(CheckStatusEnum))
                .OfType<CheckStatusEnum>().ToList();

            return new GateRecordFilters(deviceTypes, devices, ticketTypes, boats, operators, checkStatuses);
        }
    }
}
